package com.tangr.normaladapter.recyclerview;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.tangr.normaladapter.R;


/**
 * Created by tangr on 16/6/9.
 */
public class PullRecyclerView extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private final static int STATE_NORMAL = 0;
    private final static int STATE_LOAD = 1;
    private final static int STATE_REFRESH = 2;
    private int mCurrentState = STATE_NORMAL;
    private OnRecyclerRefreshListener listener;
    private RecyclerAdapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    public PullRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    public PullRecyclerView(Context context) {
        super(context);
        initView();
    }

    public PullRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_pullrecyclerview,this,true);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_dark, android.R.color.holo_red_dark, android.R.color.holo_orange_dark, android.R.color.holo_green_dark);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView = (RecyclerView) findViewById(R.id.pullrecyclerview);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int last = 0;
                int count = 0;
                boolean isLast = false;
                if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
                    final LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    last = manager.findLastCompletelyVisibleItemPosition();
                    count = manager.getItemCount();
                    isLast = last == count - 1 ? true : false;
                }else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager){
                    final StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
                    int[] positions = null;
                    positions = manager.findLastCompletelyVisibleItemPositions(positions);
//                    last = positions[0];
                    count = manager.getItemCount();
                    for (int i=0;i<positions.length;i++){
                        if(positions[i] == count - 1){
                            isLast = true;
                            break;
                        }
                    }
                }
                if (mCurrentState == STATE_NORMAL && isLast) {
                    mCurrentState = STATE_LOAD;
                    adapter.onLoadMoreStateChanged(true);
                    listener.onLoadMore();
                }
            }
        });
    }

    public void setLayoutManager(RecyclerView.LayoutManager manager) {
        this.mLayoutManager = manager;
        mRecyclerView.setLayoutManager(manager);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration decoration) {
        if (decoration != null) {
            mRecyclerView.addItemDecoration(decoration);
        }
    }

    public void setAdapter(RecyclerAdapter adapter) {
        this.adapter = adapter;
        adapter.setEnableLoadmore(true);
        mRecyclerView.setAdapter(adapter);
    }

    public void setOnRefreshListener(OnRecyclerRefreshListener onRefreshListener){
        this.listener = onRefreshListener;
    }

    public void loadFinshed() {
        adapter.onLoadMoreStateChanged(false);
        mCurrentState = STATE_NORMAL;
    }

    public void refreshFinished(){
        mSwipeRefreshLayout.setRefreshing(false);
        mCurrentState = STATE_NORMAL;
    }

    public void setRefreshing() {
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mCurrentState = STATE_REFRESH;
        mSwipeRefreshLayout.setRefreshing(true);
        listener.onRefresh();
    }

    public interface OnRecyclerRefreshListener {
        void onRefresh();
        void onLoadMore();
    }
}
